package com.oscar.ehome.view.messageview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.bean.loginbean.UserBean;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/6/29
 * 筛选框
 */

public class PopWindowSizerActivity extends Activity {

    //系统通知
    @BindView(R.id.system_message)
    CheckBox systemMessage;//5: 系统
    // 激励消息
    @BindView(R.id.exciting_message)
    CheckBox excitingMessage;//2: 激励
    // 会议通知
    @BindView(R.id.meeting_message)
    CheckBox meetingMessage;//4: 会议
    // 产品消息
    @BindView(R.id.product_message)
    CheckBox productMessage;//3:产品
    //通知
    @BindView(R.id.inform_message)
    CheckBox informMessage;//1: 通知
    // 已读
    @BindView(R.id.already_message)
    CheckBox alreadyMessage;
    // 未读
    @BindView(R.id.unread_message)
    CheckBox unreadMessage;
    // 确定
    @BindView(R.id.message_cofirm)
    Button messageCofirm;
    // 发布通知
    @BindView(R.id.releasing_buton)
    RelativeLayout releasingButon;
    // 发布图标
    @BindView(R.id.fabu_img)
    ImageView fabu_img;
    @BindView(R.id.fabu_text)
    TextView fabuText;

    List<String> types_list;//消息筛选类型  文字
    List<String> type_list;//消息筛选类型

    List<String> list_read = new ArrayList<>();//消息筛选类型


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diaglog_messagesizer);
        ButterKnife.bind(this);
        setColor();
        initData();
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        TintManager.setTints(systemMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTints(excitingMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTints(meetingMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTints(productMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTints(informMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTints(alreadyMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTints(unreadMessage, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTint(messageCofirm, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTint(fabu_img, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        TintManager.setTint(releasingButon, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        fabuText.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
    }

    public void initData() {

        String userBeans = SystemConfigFactory.getInstance(PopWindowSizerActivity.this).getSystemConfig().getUserBean();
        UserBean userbean = new Gson().fromJson(userBeans, UserBean.class);
        if (userbean != null) {
            if ("1".equals(userbean.getIsReleaseOaNotify())) {//显示发布消息
                releasingButon.setVisibility(View.VISIBLE);
            } else {
                releasingButon.setVisibility(View.GONE);
            }
        }

        if (!CommonUtils.readFlag.equals("")) {//已读未读
            list_read = CommonUtils.stringToList(CommonUtils.readFlag);
            for (int i = 0; i < list_read.size(); i++) {
                if (list_read.get(i).equals("1")) {
                    alreadyMessage.setChecked(true);
                } else if (list_read.get(i).equals("0")) {
                    unreadMessage.setChecked(true);
                }
            }
        }
        if (!CommonUtils.type.equals("")) {//类型
            type_list = CommonUtils.stringToList(CommonUtils.type);
            for (int i = 0; i < type_list.size(); i++) {
                if (type_list.get(i).equals("1")) {
                    informMessage.setChecked(true);
                } else if (type_list.get(i).equals("2")) {
                    excitingMessage.setChecked(true);
                } else if (type_list.get(i).equals("3")) {
                    productMessage.setChecked(true);
                } else if (type_list.get(i).equals("4")) {
                    meetingMessage.setChecked(true);
                } else if (type_list.get(i).equals("5")) {
                    systemMessage.setChecked(true);
                }
            }
        }
    }

    /**
     * 设置弹出位置
     */
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        //窗口对齐屏幕宽度
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.x = getResources().getDimensionPixelSize(
                R.dimen.pad_height_0);
        lp.y = getResources().getDimensionPixelSize(
                R.dimen.pad_height_48);
        lp.gravity = Gravity.TOP;//设置对话框置顶显示
        win.setAttributes(lp);
    }

    @OnClick({R.id.message_cofirm, R.id.releasing_buton})
    public void OnClickView(View view) {
        switch (view.getId()) {
            case R.id.message_cofirm://确定
                isChecked();
                CommonUtils.pop = 2;
                finish();
                overridePendingTransition(R.anim.menushow, R.anim.menuhide);
                break;
            case R.id.releasing_buton://发布通知
                Intent intent = new Intent(this, IssueMessageActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }


    /**
     * 判断类型
     */
    public void isChecked() {

        types_list = new ArrayList<>();//消息筛选类型  文字
        type_list = new ArrayList<>();//消息筛选类型

        if (unreadMessage.isChecked() && alreadyMessage.isChecked()) {//已读未读
            CommonUtils.readFlag = "0,1";
            CommonUtils.readFlags = "已读、未读";
        } else if (alreadyMessage.isChecked()) {
            CommonUtils.readFlag = "1";
            CommonUtils.readFlags = "已读";
        } else if (unreadMessage.isChecked()) {
            CommonUtils.readFlag = "0";
            CommonUtils.readFlags = "未读";
        } else {
            CommonUtils.readFlag = "";
            CommonUtils.readFlags = "";
        }

        if (informMessage.isChecked()) {//筛选类型
            type_list.add("1");
            types_list.add("通知");
        }
        if (excitingMessage.isChecked()) {
            type_list.add("2");
            types_list.add("激励");
        }
        if (productMessage.isChecked()) {
            type_list.add("3");
            types_list.add("产品");
        }
        if (meetingMessage.isChecked()) {
            type_list.add("4");
            types_list.add("会议");
        }
        if (systemMessage.isChecked()) {
            type_list.add("5");
            types_list.add("系统");
        }

        if (type_list.size() > 0) {//类型
            CommonUtils.type = CommonUtils.ListToString(type_list, 1);
            CommonUtils.types = CommonUtils.ListToString(types_list, 2);
        } else {
            CommonUtils.types = "";
            CommonUtils.type = "";
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        overridePendingTransition(R.anim.menushow, R.anim.menuhide);
    }
}
